package com.pureut.quality.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pureut.common.core.exception.GlobalException;
import com.pureut.common.core.utils.StringUtils;
import com.pureut.common.core.utils.bean.BeanUtils;
import com.pureut.common.security.utils.DictUtils;
import com.pureut.common.security.utils.SecurityUtils;
import com.pureut.quality.domain.AuditNonconformity;
import com.pureut.quality.domain.ExperimentalTemplateSettings;
import com.pureut.quality.domain.dto.AuditNonconformityDto;
import com.pureut.quality.domain.dto.ExperimentalTemplateSettingsDto;
import com.pureut.quality.domain.vo.AuditNonconformityVo;
import com.pureut.quality.domain.vo.ExperimentalTemplateSettingsVo;
import com.pureut.quality.domain.vo.SubmitVo;
import com.pureut.quality.mapper.AuditNonconformityMapper;
import com.pureut.quality.mapper.ExperimentalTemplateSettingsMapper;
import com.pureut.quality.service.AuditNonconformityService;
import com.pureut.quality.service.ExperimentalTemplateSettingsService;
import com.pureut.system.api.FeignService;
import com.pureut.system.api.domain.SysDictData;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/***
 * 实验模板设置与数据录入
 * Author:C
 * Date:2023/01/19 17:15
 * @DESC
 */
@Service
public class ExperimentalTemplateSettingsServiceImpl extends ServiceImpl<ExperimentalTemplateSettingsMapper, ExperimentalTemplateSettings> implements ExperimentalTemplateSettingsService {

    /**
     * 实验模板设置与数据录入列表 数据层
     */
    @Resource
    ExperimentalTemplateSettingsMapper experimentalTemplateSettingsMapper;

    /**
     * 远程调用 实现层
     */
    @Resource
    FeignService feignService;

    /**
     * 查询实验模板设置与数据录入列表
     * @param experimentalTemplateSettingsVo
     * @return
     */
    @Override
    public List<ExperimentalTemplateSettingsDto> selectExperimentalTemplateList(ExperimentalTemplateSettingsVo experimentalTemplateSettingsVo) {

        //模板类型（1、常规检验模板，2、环境实验模板，3、飞行测试模板）
        List<SysDictData> templateType = DictUtils.getDictCache("experimental_template_type");
        Map<String, String> templateTypeMap = templateType.stream().collect(Collectors.toMap(SysDictData::getDictValue, SysDictData::getDictLabel));

        //模板版本（下拉）
        List<SysDictData> templateVersion = DictUtils.getDictCache("template_version");
        Map<String, String> templateVersionMap = templateVersion.stream().collect(Collectors.toMap(SysDictData::getDictValue, SysDictData::getDictLabel));

        //常规检验项目
        List<SysDictData> conventionScenarios = DictUtils.getDictCache("convention_scenarios");
        Map<String, String> conventionScenariosMap = conventionScenarios.stream().collect(Collectors.toMap(SysDictData::getDictValue, SysDictData::getDictLabel));

        //环境检验项目
        List<SysDictData> environmentScenarios = DictUtils.getDictCache("environment_scenarios");
        Map<String, String> environmentScenariosMap = environmentScenarios.stream().collect(Collectors.toMap(SysDictData::getDictValue, SysDictData::getDictLabel));

        //飞行检验项目
        List<SysDictData> flightScenarios = DictUtils.getDictCache("flight_scenarios");
        Map<String, String> flightScenariosMap = flightScenarios.stream().collect(Collectors.toMap(SysDictData::getDictValue, SysDictData::getDictLabel));

        List<ExperimentalTemplateSettingsDto> list = experimentalTemplateSettingsMapper.selectExperimentalTemplateList(experimentalTemplateSettingsVo);
        for (ExperimentalTemplateSettingsDto dto : list) {
            // 模板版本
            dto.setTemplateVersion(templateVersionMap.get(dto.getTemplateVersion()));
            // 常规检验项目
            if (!StringUtils.isEmpty(dto.getInspectionItems())){
                //模板类型（1、常规检验模板，2、环境实验模板，3、飞行测试模板）
                switch (dto.getType()) {
                    case "1":
                        dto.setInspectionItems(conventionScenariosMap.get(dto.getInspectionItems()));
                        break;
                    case "2":
                        dto.setInspectionItems(environmentScenariosMap.get(dto.getInspectionItems()));
                        break;
                    case "3":
                        dto.setInspectionItems(flightScenariosMap.get(dto.getInspectionItems()));
                        break;
                }
            }
            // 模板类型
            dto.setType(templateTypeMap.get(dto.getType()));
        }
        return list;
    }

    /**
     * 回显
     * @param id
     * @return
     */
    @Override
    public ExperimentalTemplateSettingsDto getInfo(Long id) {
        return experimentalTemplateSettingsMapper.getInfo(id);
    }

    /**
     * 新增实验模板设置与数据录入
     * @param experimentalTemplateSettingsVo
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean insertExperimentalTemplate(ExperimentalTemplateSettingsVo experimentalTemplateSettingsVo) {
        boolean save = false;
        // 保存状态（1、新增，2、修改）
        if (experimentalTemplateSettingsVo.getSaveStatus().equals("1")){
            String authorityCoding = feignService.getAuthorityCoding("experimentManage:experimentTemplate:list");
            if (authorityCoding == null) {
                throw new GlobalException("该单据未进行配置，无法生成单据编码");
            }
            ExperimentalTemplateSettings experimentalTemplateSettings = new ExperimentalTemplateSettings();
            BeanUtils.copyProperties(experimentalTemplateSettingsVo, experimentalTemplateSettings);
            experimentalTemplateSettings.setId(experimentalTemplateSettingsVo.getId());
            experimentalTemplateSettings.setCreateBy(SecurityUtils.getUsername());
            experimentalTemplateSettings.setCreateTime(new Date());
            experimentalTemplateSettings.setSampleNumber(authorityCoding);
             save = save(experimentalTemplateSettings);
        } else {
            ExperimentalTemplateSettings experimentalTemplateSettings = new ExperimentalTemplateSettings();
            BeanUtils.copyProperties(experimentalTemplateSettingsVo, experimentalTemplateSettings);
            experimentalTemplateSettings.setId(experimentalTemplateSettingsVo.getId());
            experimentalTemplateSettings.setUpdateBy(SecurityUtils.getUsername());
            experimentalTemplateSettings.setUpdateTime(new Date());
            save = updateById(experimentalTemplateSettings);
        }
        return save;
    }

    /**
     * 修改实验模板设置与数据录入
     * @param experimentalTemplateSettingsVo
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateExperimentalTemplate(ExperimentalTemplateSettingsVo experimentalTemplateSettingsVo) {
        ExperimentalTemplateSettings experimentalTemplateSettings = new ExperimentalTemplateSettings();
        BeanUtils.copyProperties(experimentalTemplateSettingsVo, experimentalTemplateSettings);
        experimentalTemplateSettings.setId(experimentalTemplateSettingsVo.getId());
        experimentalTemplateSettings.setUpdateBy(SecurityUtils.getUsername());
        experimentalTemplateSettings.setUpdateTime(new Date());
        return updateById(experimentalTemplateSettings);
    }

    /**
     * 删除实验模板设置与数据录入
     * @param id
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean removeExperimentalTemplate(String id) {
        return removeById(id);
    }
}
